/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sysmo.server.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author Derek
 */
public class DoCustomGraphAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String startTimeStr = request.getParameter("start");
        String endTimeStr = request.getParameter("end");
        Long startTime, endTime;
        Long now = new Date().getTime();
        SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            startTime = fmt1.parse(startTimeStr).getTime();
        } catch (ParseException e) {
            startTime = fmt2.parse(startTimeStr).getTime();
        } catch (Exception e) {
            startTime = now - 3600 * 1000;
        }
        try {
            endTime = fmt1.parse(endTimeStr).getTime();
        } catch (ParseException e) {
            endTime = fmt2.parse(endTimeStr).getTime();
        } catch (Exception e) {
            endTime = now;
        }
        request.setAttribute("startTime", startTime);
        request.setAttribute("endTime", endTime);
        return mapping.findForward(SUCCESS);
    }
}
